<?php

/*
 * 会员管理
 vip_state  账号状态
 */

class VipAction extends Action {

    public $vips;

    public function __construct() {
        parent::__construct();
        $this->vips = M('vips');
        R("/Login/ifsession");
    }
    //会员列表
    public function viplist() {
        import("ORG.Util.Page");
        $count = $this->vips->count();
        $listrow = 15;
        $page = new Page($count, $listrow);
        $show = $page->show();
        $arr_list = $this->vips->order('vip_id')->limit($page->firstRow . "," . $page->listRows)->select();
        $this->assign('lists', $arr_list);
        $this->show = $show;
        $this->display();
    }
	public function edit() {
		if (IS_GET) {
			$data = I('get.');
			$vip = $this->vips->where('vip_id="' . $data['vip_id'] . '"')->find();
			$this->assign('vip',$vip);
			$this->display();
		}
		if (IS_POST) {
			//导入图片上传类  
            import("ORG.Net.UploadFile");  
            //实例化上传类  
            $upload = new UploadFile();
			//设置文件上传大小 
            $upload->maxSize = 3145728;  
            //设置文件上传类型  
            $upload->allowExts = array('jpg','gif','png','jpeg');  
            //设置文件上传位置  
            $upload->savePath = "./Public/Uploads/";//这里说明一下，由于ThinkPHP是有入口文件的，所以这里的./Public是指网站根目录下的Public文件夹  
            //设置文件上传名(按照时间)  
            $upload->saveRule = "time";
			 //设置需要生成缩略图，仅对图像文件有效   
			$upload->thumb = true;   
			// 设置引用图片类库包路径   
			$upload->imageClassPath = 'ORG.Util.Image';   
			//设置需要生成缩略图的文件后缀   
			$upload->thumbPrefix = 'm_,s_';  //生产2张缩略图   
			//设置缩略图最大宽度   
			$upload->thumbMaxWidth = '400,50';   
			//设置缩略图最大高度   
			$upload->thumbMaxHeight = '400,50';   
			//设置上传文件规则   
			$upload->saveRule = uniqid;   
			//删除原图   
			$upload->thumbRemoveOrigin = true;   
            if (!$upload->upload()){  
                $this->error($upload->getErrorMsg());  
            }else{  
                //上传成功，获取上传信息  
                $info = $upload->getUploadFileInfo();  
            }  
  
            //保存表单数据，包括上传的图片  
            $this->vips->create();  
            $image = $info[0]['savename'];
			//var_dump($image);die();
			$datas['vip_id'] = $_POST['vip_id'];
			$date['old_vip_pic'] = $_POST['old_vip_pic'];//旧的会员头像
			//var_dump($date['old_vip_pic']);
			$datas['vip_pic'] = $image;
			$datas['vipname'] = $_POST['vipname'];
			$datas['sex'] = $_POST['sex'];
			$datas['age'] = $_POST['age'];
			$datas['regphone'] = $_POST['regphone'];
			 if ($this->vips->where('vip_id="' . $datas['vip_id'] . '"')->save($datas)) {
				@unlink('./Public/Uploads/'.'s_'.$date['old_vip_pic']);
				@unlink('./Public/Uploads/'.'m_'.$date['old_vip_pic']);
                $this->success('修改成功', U('Vip/viplist'));
            } else {
                $this->error('修改失败', U('Vip/viplist'));
            }
		}
			
	}

	public function state($vip_id) {
		$data = $this->vips->where('vip_id="' . $vip_id . '"')->find();
		//var_dump($data);die();
		if($data['vip_state']== 0){
			$data['vip_state'] = 1;
			$this->vips->where('vip_id="' . $vip_id . '"')->save($data);
			$this->success('禁用成功', U('Vip/viplist'));
		}else{
			$data['vip_state'] = 0;
			$this->vips->where('vip_id="' . $vip_id . '"')->save($data);
			$this->success('启用成功', U('Vip/viplist'));
		}
	}
}
